
import store from '@/store/getters';
import user from '@/store/modules/user';


/*
    DICT 各类项目流程的配置
*/


/**
 *  中转列表 类型
 *  作为中转列表的 商机列表    DrawupTransitonType_SJ
 *  作为选择的 商机列表
 *  作为选择的 会议纪要列表
 */
export const DrawupTransitonType_SJ = 'DrawupTransitonType_SJ';
export const DrawupChooseType_SJ = 'DrawupChooseType_SJ';
export const DrawupChooseType_HYJY = 'DrawupChooseType_HYJY';




// --------------  本地缓存的 key 值

/**
 *  选中 商机 后，缓存本地，带到其他界面使用
 *  缓存本地 使用的key 值
 * */
export const ChooseSJ_KEY = 'ChooseSJ_KEY';

/**
 *  选中 OA系统会议纪要，缓存本地，带到其他界面使用
 *  缓存本地 使用的key 值
 * */
export const Choose_HYJY_KEY = 'Choose_HYJY_KEY';

/**
 *  选中 DICT系统会议纪要，缓存本地，带到其他界面使用
 *  缓存本地 使用的key 值
 * */
export const Choose_HYJY_DICT_KEY = 'Choose_HYJY_DICT_KEY';

/**
 *  新增 里程碑节点信息，缓存本地，带到其他界面使用
 *  缓存本地 使用的key 值
 * */
export const Append_lcb_KEY = 'Append_lcb_KEY';









/**
 *  DICT 各类项目流程 code标识
 *  前后必须保持一致，因为 dict_xmjd_code 在做 key 的时候会自己转成字符串
 */
export const dict_ybzc_code = 'dict_ybzc_code';    /// 应标支撑
export const dict_fazc_code = 'dict_fazc_code';    /// 方案支撑
export const dict_sqbf_code = 'dict_sqbf_code';    /// 售前拜访
export const dict_sjwh_code = 'dict_sjwh_code';    /// 新增商机-商机维护

export const dict_xmjd_code = 'dict_xmjd_code';    /// 项目进度
export const dict_xmkg_code = 'dict_xmkg_code';    /// 项目开工
export const dict_xmbg_code = 'dict_xmbg_code';    /// 项目变更
export const dict_xmjj_code = 'dict_xmjj_code';    /// 项目交接
// export const dict_5gxm_code = 'dict_5gxm_code';    /// 5G 项目管理
export const dict_sjzxm_code = 'dict_sjzxm_code';  /// 商机转项目
export const dict_shystb_code = 'dict_shystb_code';  /// 合同计划列表->三化预算填报

export const dict_sjyp_code = 'dict_sjyp_code';    /// 商机研判
const jDrawupUtil = {

    /**
     *  手机端 支持起草的项目集合
     */
    appCanDrawupProcessMap: {

        dict_ybzc_code: { code: dict_ybzc_code, value: '应标支撑', permissionCode: 'preSale:answer' },
        dict_fazc_code: { code: dict_fazc_code, value: '方案支撑', permissionCode: 'preSale:scheme' },
        dict_sqbf_code: { code: dict_sqbf_code, value: '售前拜访', permissionCode: 'business:visit' },
        dict_sjwh_code: { code: dict_sjwh_code, value: '商机维护', permissionCode: 'business:management' },


        dict_xmjd_code: { code: dict_xmjd_code, value: '项目进度', permissionCode: 'preSale:initProjectProcess' },
        dict_xmkg_code: { code: dict_xmkg_code, value: '项目开工', permissionCode: 'preSale:initProjectStart' },
        dict_xmbg_code: { code: dict_xmbg_code, value: '项目变更', permissionCode: 'preSale:projectChange' },
        dict_xmjj_code: { code: dict_xmjj_code, value: '项目交接', permissionCode: 'preSale:projecthandover' },
        dict_sjzxm_code: { code: dict_sjzxm_code, value: '商机转项目', permissionCode: 'preSale:transitionProject' },
        // dict_5gxm_code: { code: dict_5gxm_code, value: '5G项目管理', permissionCode: 'preSale:fiveGvertical' },
        dict_ca_borrow: { code: 'dict_ca_borrow', value: 'CA借用', permissionCode: 'preSale:caReturn' },
        dict_ca_return: { code: 'dict_ca_return', value: 'CA归还', permissionCode: 'preSale:caBorrow' },
        dict_shystb_code: { code: dict_shystb_code, value: '三化预算填报', permissionCode: 'preSale:contractPlan' },

        dict_sjyp_code: { code: dict_sjyp_code, value: '商机研判', permissionCode: 'select:judged' },
    },

    // ----------------  发起起草

    /**
     *  当前用户 可以起草的项目集合
     */
    currentUserCanDrawupNameList: [],
    currentUserCanDrawupCodeList: [],
    currentUserCanDrawupProcessNames() {

        if (this.currentUserCanDrawupNameList.length > 0) {
            return this.currentUserCanDrawupNameList;
        }

        // 本地写死 当前已经代码接入可以起草的应用
        let appCanDrawupList = Object.values(this.appCanDrawupProcessMap);

        // 网络返回 当前用户可以起草的所有应用
        let authPermissions = user.state.userinfo.authPermissions;

        // 综合 当前用户真实可以起草的应用
        // 商机维护、售前拜访、方案支撑、应标支撑、CA借用、商机转项目、项目交接、项目开工、项目进度
        let nameList = ['商机维护', '售前拜访', '方案支撑', '应标支撑', 'CA借用', 'CA归还', '商机转项目', '项目交接',
            '项目开工', '项目进度', '项目变更','5G项目管理', '三化预算填报','商机研判'];

        for (const name of nameList) {
            
            for (let appItem of appCanDrawupList) {

                if (appItem.value === name) {

                    for (let item of authPermissions) {
                        
                        if (item.permissionCode === appItem.permissionCode) {

                            console.log('⭕️⭕️⭕️', appItem.value, appItem.code);
                            this.currentUserCanDrawupNameList.push(appItem.value);
                            this.currentUserCanDrawupCodeList.push(appItem.code);
                        }
                    }
                }
            }
        }

        return this.currentUserCanDrawupNameList;
    },

    /**
     *  当前用户 对该流程是否有起草权限
     */
    currentUserHasDrawupEable(value) {

        let canDrawupList = currentUserCanDrawupProcess();
        let canDraw = false;
        for (let processName of canDrawupList) {
            if (processName === value) {
                canDraw = true
            }
        }

        return canDraw;
    },

    // ----------------  中转列表 （商机）

    /** 
        *  根据 code标识，获取流程的名字
        */
    getProcessName(ID) {

        return this.appCanDrawupProcessMap[ID].value;
    },

    /**
     *  前往商机列表的参数
     */
    transitionParamMap: {

        dict_xmjd_code: { isDrawup: '1', processID: dict_xmjd_code, transType: DrawupTransitonType_SJ },
        dict_xmkg_code: { isDrawup: '1', processID: dict_xmkg_code, transType: DrawupTransitonType_SJ },
        dict_xmbg_code: { isDrawup: '1', processID: dict_xmbg_code, transType: DrawupChooseType_SJ },
        dict_sqbf_code: { isDrawup: '1', processID: dict_sqbf_code, transType: DrawupTransitonType_SJ },
        dict_sjwh_code: { isDrawup: '1', processID: dict_sjwh_code, transType: DrawupTransitonType_SJ },
        dict_xmjj_code: { isDrawup: '1', processID: dict_xmjj_code, transType: DrawupTransitonType_SJ },
        // dict_5gxm_code: { isDrawup: '1', processID: dict_5gxm_code, transType: DrawupTransitonType_SJ },

        dict_sjzxm_code: { isDrawup: '1', processID: dict_sjzxm_code },
        dict_shystb_code: { isDrawup: '1', processID: dict_shystb_code, transType: DrawupTransitonType_SJ },
        dict_sjyp_code: { isDrawup: '1', processID: dict_sjyp_code, transType: DrawupTransitonType_SJ },
    },

    /** 
     *  根据 code标识，获取 商机列表的 URL 片段
     */
    getSJListURLWithCode(ID) {

        if (ID === dict_xmjd_code) {
            return 'projectProcessModel$api$findAllCustom';
        }
        else if (ID === dict_xmkg_code) {
            return 'projectStartModel$api$findAllCustom';
        }
        else if (ID === dict_xmbg_code) {
            return 'projectChangeModel$api$findAllCustom';
        }
        else if (ID === dict_xmjj_code) {
            return 'projectInformation$api$findAllList';
        }
        else if (ID === dict_sqbf_code) {
            return 'businessVisit$api$findAllCustom';
        }
        else if (ID === dict_sjwh_code) {
            return 'businessAdd$api$findAllList';
        }
        else if (ID === dict_shystb_code) {
            return 'esopBusinessToProject$api$findAllList';
        }
        else if (ID === dict_sjyp_code) {
            return 'businessJudged$api$findAllBusinessJudged';
        }
        return '';
    },



    /** 
     *  根据 流程的名字汉字，获取 前往商机列表的参数
     */
    getPushTransitionTableParam(value) {

        if (this.currentUserCanDrawupNameList.length == 0) {
            console.log('⭕️⭕️⭕️ 机列表的参数 失败');
            return {};
        }

        let code = this.currentUserCanDrawupCodeList[this.currentUserCanDrawupNameList.indexOf(value)];
        let pushParams = this.transitionParamMap[code];
        console.log('⭕️⭕️⭕️ 商机列表的参数', code, pushParams);

        pushParams.pmInsType = this.getPmInstypeWithCode(code);
        pushParams.location = 'dict.start';
        return pushParams;
    },


    /**
     *  根据 流程 code标识，获取当前人起草时真实的 pmInsType
     */
    getPmInstypeWithCode(code) {

        let pmInsType = ''
        let belongCompanyTypeDictValue = user.state.userinfo.belongCompanyTypeDictValue;
        if (code === dict_xmjd_code) {

            /// 项目进度 省 - SJ、市 - SK、县区 - SL
            if (belongCompanyTypeDictValue == '01') {
                pmInsType = 'SJ'
            }
            else if (belongCompanyTypeDictValue == '02') {
                pmInsType = 'SK'
            }
            else if (belongCompanyTypeDictValue == '03') {
                pmInsType = 'SL'
            }
        }
        else if (code === dict_xmkg_code) {

            /// 项目开工 省 - SI、市 - SH、县区 - SG
            if (belongCompanyTypeDictValue == '01') {
                pmInsType = 'SI'
            }
            else if (belongCompanyTypeDictValue == '02') {
                pmInsType = 'SH'
            }
            else if (belongCompanyTypeDictValue == '03') {
                pmInsType = 'SG'
            }
        }
        else if (code === dict_xmjj_code) {

            /// 项目交接
            /// 省 - MD、市 - ME、县区 - MF
            if (belongCompanyTypeDictValue == '01') {
                pmInsType = 'MD'
            }
            else if (belongCompanyTypeDictValue == '02') {
                pmInsType = 'ME'
            }
            else if (belongCompanyTypeDictValue == '03') {
                pmInsType = 'MF'
            }
        }
        else if (code === dict_xmbg_code) {
            /// 项目变更 
            pmInsType = 'APC'
        }
        else if (code === dict_sqbf_code) {
            /// 售前拜访
            pmInsType = 'SQBF'
        }
        else if (code === dict_sjwh_code) {
            /// 商机维护
            pmInsType = 'SJWH'
        }
        else if (code === dict_sjzxm_code) {
            /// 商机转项目
            pmInsType = 'EB'
        }
        // else if (code === dict_5gxm_code) {
        //     // 5G项目管理 取消认定 - FGC、认定 - FGA
        //     let pushParams = this.transitionParamMap[code];
        //     pmInsType = pushParams.pmInsType
        // }
        else if (code === dict_shystb_code) {
            /// 三化预算填报
            pmInsType = 'SHYSTB'
        }
        else if (code === dict_sjyp_code) {
            /// 商机研判
            pmInsType = 'YPL'
        }
        return pmInsType;
    },


}

export default jDrawupUtil;







